<template>
  <div id="index">
    <div id="header">
      <div class="firstView">
        <img src="/static/images/user.png" alt="">
        <div>
          <span style="line-height: 2rem;text-align: center;font-size: 1.2rem;width: max-content;padding-left: 0.7rem">老茶馆--9号桌</span>
          <span style="line-height: 1.4rem;text-align: center;font-size: 0.9rem;width: max-content;padding-left: 0.7rem;color: #6a6a6a">用餐人数：{{peopleNumber}}人</span>
        </div>
      </div>
      <input class="Find" type="text" placeholder="请输入菜品名称" v-model="searchFood"/>
    </div>
    <div id="section">
      <div id="kindItem">
        <div class="kItem" :style="indexNumber === -1?{'background':kindItem.background,'color':kindItem.color}:{'background':'#eeeeee'}" @click="getAllMenu()">全部</div>
        <div class="kItem"
             v-for="(item,index) in menuClass"
             v-bind:key="index"
             :style="indexNumber === index?{'background':kindItem.background,'color':kindItem.color}:{'background':'#eeeeee'}"
             @click="findMenuByClass(index)">
          {{item.m_class}}
        </div>
      </div>
      <div id="mealItem">
        <div class="mItem" v-for="(item,index) in menu" v-bind:key="index">
          <img :src="item.picture" alt="">
          <div class="Info">
            <span>{{item.m_name}}</span>
            <span>数量：{{item.stock}}</span>
            <span>销量：{{item.sales_volume}}</span>
            <span>￥{{item.m_price}}.00</span>
          </div>
          <div class="order">
            <button @click="reduce(index,item.m_price)" :style="numberDetail[idItems[index]] === 0?{'visibility':'hidden'}:{'visibility':'visible'}">-</button>
            <span :style="numberDetail[idItems[index]] === 0?{'visibility':'hidden'}:{'visibility':'visible'}">{{numberDetail[idItems[index]]}}</span>
          </div>
          <button @click="add(index,item.m_price)" style="margin-top: 3.76rem;margin-left: 0.5rem;margin-right: 0.1rem">+</button>
        </div>
      </div>
    </div>
    <div class="shoppingCar" @click="shoppingCar()">
      <img src="/static/order/shoppingCar.png" alt="">
    </div>
    <div id="footer">
      <span>总计：{{totalPrice}}元，{{totalNumber}}件</span>
      <button @click="toOrderDetail()">下单</button>
    </div>
    <div id="dialog" :style="{'display': DialogVisibility}">
      <div class="header">
        <span @click="back()">返回</span>
        <span>购物车</span>
        <span @click="delAll()">清空购物车</span>
      </div>
      <div class="section">
        <div class="Item" v-for="(item, index) in shoppingDetail" :key="index" :style="numberDetail[idItems2[index]] === 0?{'display':'none'}:{'visibility':'auto'}">
          <div>
            <span>{{item.m_name}}</span>
            <span>{{item.m_price*numberDetail[idItems2[index]]}}.00</span>
          </div>
          <button @click="reduce(index,item.m_price)">-</button>
          <span>{{numberDetail[idItems2[index]]}}</span>
          <button @click="add(index,item.m_price)">+</button>
          <button @click="delThis(index,item.m_price)">x</button>
        </div>
      </div>
    </div>
    <div :style="{'display': chooseNumberDialogVisibility}" id="choosePeopleNumber">
      <span class="title">请选择用餐人数</span>
      <div class="peopleNumber">
        <div>
          <span @click="choosePeopleNumber(1)">1</span>
          <span @click="choosePeopleNumber(2)">2</span>
          <span @click="choosePeopleNumber(3)">3</span>
          <span style="margin-right: 0"  @click="choosePeopleNumber(4)">4</span>
        </div>
        <div>
          <span @click="choosePeopleNumber(5)">5</span>
          <span @click="choosePeopleNumber(6)">6</span>
          <span @click="choosePeopleNumber(7)">7</span>
          <span style="margin-right: 0" @click="choosePeopleNumber(8)">8</span>
        </div>
        <div>
          <span @click="choosePeopleNumber(9)">9</span>
          <span @click="choosePeopleNumber(10)">10</span>
          <span @click="choosePeopleNumber(11)">11</span>
          <span style="margin-right: 0" @click="choosePeopleNumber(12)">12</span>
        </div>
      </div>
      <div class="byUser">
        <span>自定义 &nbsp&nbsp&nbsp</span>
        <input type="text" v-model="peopleNumber"/>
      </div>
      <span style="margin-right: auto;margin-left: auto;margin-top:0.2rem;font-size: 0.8rem;color: red;" :style="{'visibility':reminder}">请选择用餐人数</span>
      <button @click="beginOrder()">开始点餐</button>
    </div>
  </div>
</template>

<script>
  import request from '../../utils/request'
  export default {
    name: "index",
    data(){
      return{
        number: 0,
        DialogVisibility: 'none',
        chooseNumberDialogVisibility: 'auto',
        shoppingDetail:[],
        totalNumber: 0,
        totalPrice: 0,
        kindItem: {
          background: 'white',
          color: '#ff4800'
        },
        peopleNumber: '',
        menu:[],
        backupMenu:[],
        numberDetail: [],
        delNumber:[],
        menuClass:[],
        indexNumber: -1,
        idItems:[],
        idItems2:[],
        firstDish: 0,
        searchFood: '',
        reminder: 'hidden',
        nowOrderId:'o001'
      }
    },
    methods:{
      shoppingCar(){
        if(this.DialogVisibility === 'none'){
          this.DialogVisibility = 'auto';
        }else {
          this.DialogVisibility = 'none';
        }
      },
      back(){
        this.DialogVisibility = 'none';
      },
      add(key,price){
        price = Number(price);
        this.totalPrice = this.totalPrice+price;
        this.totalNumber++;
        this.numberDetail[this.idItems[key]]++;
        // console.log(this.numberDetail);
      },
      reduce(key,price) {
        price = Number(price);
        this.totalPrice = this.totalPrice-price;
        this.totalNumber--;
        this.numberDetail[this.idItems[key]]--;
        // console.log(this.numberDetail);
      },
      delThis(key,price){
        price = Number(price);
        this.totalPrice = this.totalPrice-price*this.numberDetail[this.idItems[key]];
        this.totalNumber = this.totalNumber-this.numberDetail[this.idItems[key]];
        this.numberDetail[this.idItems[key]] = 0;
      },
      delAll(){
        this.totalPrice = 0;
        this.totalNumber = 0;
        this.numberDetail = {...this.delNumber}
        // console.log(this.numberDetail)
      },
      choosePeopleNumber(number){
        this.peopleNumber = number
      },
      toOrderDetail(){
        if(this.nowOrderId === ''){
          this.$axios.get("getOrderNumber").then(res=>{
            let number = res.data;
            this.nowOrderId = 'o00'+ number.toString();
            console.log(this.nowOrderId)
          });
        }else{
          let bodyData = {
            totalPrice: 0,
            O_id: this.nowOrderId,
            M_id: [],
            M_number: []
          };
          for(let i = 0;i<this.shoppingDetail.length;i++){
            console.log(i,this.numberDetail[this.idItems2[i]]);
            if(this.numberDetail[this.idItems2[i]] !== 0){
              bodyData.totalPrice = bodyData.totalPrice + this.shoppingDetail[i].m_price*this.numberDetail[this.idItems2[i]];
              bodyData.M_number.push(this.numberDetail[this.idItems2[i]]);
              bodyData.M_id.push(this.shoppingDetail[i].m_id)
            }
          }
          console.log(bodyData.M_number);
          console.log(bodyData.M_id);
          let that = this;
          wx.request({
            url: 'http://localhost:8081/updateOrder', //仅为示例，并非真实的接口地址
            data: {
              bodyData
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            success (res) {
              console.log(res.data)
              wx.navigateTo({
                url:'/pages/orderDetail/main?id='+that.nowOrderId.toString()
              });
            }
          });
        }
        this.delAll();
        this.getAllMenu();
      },
      beginOrder(){
        // console.log(this.peopleNumber);
        if(this.peopleNumber === ''){
          this.reminder = 'visible'
        }else {
          this.chooseNumberDialogVisibility = 'none'
        }
      },
      getNumber(){
        let i = 0;
        this.menu.forEach((value,index)=>{
          if(value['m_id'] === this.allMenu[i].m_id){
            value['orderNumber'] = this.allMenu[i].orderNumber;
          }else{
            value['orderNumber'] = 0;
          }
        });
      },
      findMenuByClass(index){
        this.searchFood = '';
        let data = {class_id:(index+1)};
        this.$axios.get("getMenuByClass",data).then(res=>{
          this.menu = res.data;
          // console.log(this.menu);
          let i = this.menu.length;
          this.idItems = [];
          while(i>0){
            i--;
            this.idItems[i] = {...this.menu[i]}.m_id.toString();
            this.idItems[i] = this.idItems[i].replace(/[^0-9]/ig,"");
            this.idItems[i] = parseInt(this.idItems[i])-1;
          }
          // console.log("idItem",this.idItems);
          this.indexNumber = index;
        });
      },
      getAllMenu(){
        this.searchFood = '';
        this.indexNumber = -1;
        this.firstDish = 0;
        this.$axios.get('menu').then(res=>{
          this.menu = res.data;
          let i = this.menu.length;
          while(i>0){
            i--;
            this.idItems[i] = {...this.menu[i]}.m_id.toString();
            this.idItems[i] = this.idItems[i].replace(/[^0-9]/ig,"");
            this.idItems[i] = parseInt(this.idItems[i])-1;
          }
          // console.log("idItem",this.idItems);
          // console.log(this.menu);
        });
      }
    },
    mounted(){
      this.$axios.get("class").then(res=>{
        this.menuClass = res.data;
        // console.log(this.menuClass)
      });
      this.$axios.get('menu').then(res=>{
        this.menu = res.data;
        this.shoppingDetail = res.data;
        this.backupMenu = res.data;
        let i = this.menu.length;
        let n = {...this.menu[i-1]}.m_id.toString();
        n = n.replace(/[^0-9]/ig,"");
        n = parseInt(n);
        while(n>0){
          n--;
          this.delNumber[n] = 0;
          this.numberDetail[n] = 0;
        }
        while(i>0){
          i--;
          this.idItems[i] = {...this.menu[i]}.m_id.toString();
          this.idItems[i] = this.idItems[i].replace(/[^0-9]/ig,"");
          this.idItems[i] = parseInt(this.idItems[i])-1;
        }
        this.idItems2 = this.idItems;
        this.numberDetail = {...this.numberDetail};
        // console.log(this.numberDetail);
        // console.log("idItem",this.idItems);
        // console.log(this.menu);
      });
      if(this.peopleNumber !== ''){
        this.chooseNumberDialogVisibility = 'none'
      }
    },
    watch:{
      'searchFood':{
        immediate:true,
        handler(val){
          // console.log(val);
          let n=0;
          let a=[];
          for(let i=0;i<this.backupMenu.length;i++){
            if(this.backupMenu[i].m_name.indexOf(val) !== -1){
              a[n] = this.backupMenu[i];
              n++;
            }
          }
          this.menu = a;
          this.idItems = [];
          let i = this.menu.length;
          while(i>0){
            i--;
            this.idItems[i] = {...this.menu[i]}.m_id.toString();
            this.idItems[i] = this.idItems[i].replace(/[^0-9]/ig,"");
            this.idItems[i] = parseInt(this.idItems[i])-1;
          }
        }
      }
    }
  }
</script>

<style scoped>
  #index{
    width: 100%;
    height: 100%;
  }
  #header{
    background: #E5A01C;
    width: 100%;
    height: 20%;
    display: flex;
    flex-direction: column;
  }
  .firstView{
    height: min-content;
    display: flex;
    flex-direction: row;
  }
  .firstView img{
    margin-left: 0.25rem;
    width: 1.7rem;
    height: 1.3rem;
  }
  .firstView div{
    display: flex;
    flex-direction: column;
  }
  .Find{
    background: white;
    width: 92%;
    height: 0.66rem;
    color: gray;
    line-height: 0.66rem;
    text-align: center;
    margin-top: 0.2rem;
    border-radius: 1rem;
    margin-left: auto;
    margin-right: auto;
  }
  #section{
    display: flex;
    flex-direction: row;
    margin-top: 0.12rem;
    width: 100%;
    height: 73%;
  }
  #kindItem{
    overflow: scroll;
    display: flex;
    flex-direction: column;
    width: max-content;
    height: 100%;
    background: #eeeeee;
  }
  .kItem{
    width: 1.75rem;
    height: 0.7rem;
    line-height: 0.7rem;
    text-align: center;
    border: 1px solid #e3e3e3;
  }
  #mealItem{
    overflow: scroll;
    display: flex;
    flex-direction: column;
    width: max-content;
    height: 100%;
  }
  .mItem{
    width: 5.68rem;
    height: 1.8rem;
    display: flex;
    flex-direction: row;
    margin-bottom: 0.1rem;
  }
  .mItem img{
    margin: 0.2rem;
    width: 25%;
    height: 83%;
  }
  .Info{
    display: flex;
    flex-direction: column;
    width: max-content;
    margin-top: 0.1rem;
  }
  .Info span{
    width: max-content;
  }
  .Info span:nth-child(2){
    margin-left: 0.06rem;
    line-height: 0.35rem;
    color: gray;
    font-size: 0.21rem;
  }
  .Info span:nth-child(3){
    margin-left: 0.06rem;
    line-height: 0.35rem;
    color: gray;
    font-size: 0.21rem;
  }
  .Info span:nth-child(4){
    line-height: 0.5rem;
    font-size: 0.32rem;
    color: #ff4001;
    font-weight: bolder;
  }
  .mItem .order{
    margin-left: auto;
    margin-top: 1.2rem;
    width: min-content;
    height: min-content;
    display: flex;
    flex-direction: row;
  }
  .mItem .order span{
    margin-left: 0.2rem;
  }
  .mItem button{
    margin: 0;
    color: #ff4d3e;
    font-size: 0.3rem;
    border-radius: 50%;
    background: white;
    border: 1px #ff4d3e solid;
    width: 0.45rem;
    height: 0.45rem;
    line-height: 0.4rem;
    text-align: center;
    padding: 0;
  }
  .shoppingCar{
    bottom: 0.12rem;
    left: 0.15rem;
    position: absolute;
    width: 1.13rem;
    height: 1.13rem;
    border-radius: 50%;
    z-index: 2;
    background: #ff5d10;
  }
  .shoppingCar img{
    margin-top: 0.25rem;
    margin-left: 0.22rem;
    width: 0.6rem;
    height: 0.6rem;
  }
  #footer{
    width: 100%;
    height: 6%;
    display: flex;
    flex-direction: row;
  }
  #footer span{
    font-size: 0.35rem;
    margin-top: 0.15rem;
    margin-left: 2.2rem;
  }
  #footer button{
    margin-right: 0;
    padding-top: 0.2rem;
    line-height: 100%;
    text-align: center;
    color: white;
    background: #ff5d10;
    width: 1.9rem;
  }
  #dialog{
    box-shadow: 0 -6rem 3rem 3rem rgba(0,0,0,0.5);
    position: absolute;
    top: 6.5rem;
    width: 100%;
    height: 4.8rem;
    display: flex;
    flex-direction: column;
  }
  #dialog .header{
    color: white;
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 15%;
    background: #ff582e;
  }
  #dialog .header span:nth-child(1){
    font-size: 0.3rem;
    margin-left: 0.38rem;
    margin-top: 0.17rem;
  }
  #dialog .header span:nth-child(2){
    font-size: 0.36rem;
    margin-left: 2.28rem;
    margin-right: 1.1rem;
    margin-top: 0.175rem;
  }
  #dialog .header span:nth-child(3){
    font-size: 0.3rem;
    margin-left: 0.38rem;
    margin-top: 0.17rem;
  }
  #dialog .section{
    overflow: scroll;
    background: white;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 85%;
  }
  #dialog .section .Item{
    width: 99%;
    height: min-content;
    padding-bottom: 0.07rem;
    border: 1px solid #d1d1d1;
    display: flex;
    flex-direction: row;
  }
  #dialog .section .Item div{
    margin-top: 0.1rem;
    margin-left: 0.53rem;
    display: flex;
    flex-direction: column;
  }
  #dialog .section .Item div span:nth-child(1){
    font-size: 0.3rem;
  }
  #dialog .section .Item div span:nth-child(2){
    font-size: 0.3rem;
    color: #ff4800;
  }
  #dialog .section .Item button{
    margin-top: 0.25rem;
    font-weight: lighter;
    font-size: 0.4rem;
    padding: 0;
    width: 0.9rem;
    text-align: center;
    line-height: 0.45rem;
    height: 0.45rem;
    border-radius: 50%;
  }
  #dialog .section .Item button:nth-child(2){
    margin-right: 0.23rem;
    color: orange;
    border: 1px solid orange;
  }
  #dialog .section .Item span:nth-child(3){
    margin-top: 0.25rem;
    margin-right: 0.23rem;
    color: orange;
  }
  #dialog .section .Item button:nth-child(4){
    margin-right: 0.9rem;
    color: orange;
    border: 1px solid orange;
  }
  #dialog .section .Item button:nth-child(5){
    margin-right: 0.26rem;
    color: white;
    background: #afafaf;
  }
  #choosePeopleNumber{
    z-index: 3;
    position: absolute;
    display: flex;
    flex-direction: column;
    top: 2.3rem;
    left: 0.55rem;
    width: 87%;
    height: 5.7rem;
    background: white;
    border-radius: 0.28rem;
    box-shadow: 0 0 10rem 10rem rgb(0,0,0,0.5);
  }
  #choosePeopleNumber .title{
    font-size: 0.38rem;
    font-weight: bold;
    margin-left: auto;
    margin-right: auto;
    margin-top: 0.38rem;
  }
  #choosePeopleNumber .peopleNumber{
    display: flex;
    flex-direction: column;
    width: 95%;
    height: min-content;
    padding-bottom: 0.2rem;
    border-bottom: #bcbcbc 1px solid;
    margin-left: auto;
    margin-right: auto;
    margin-top: 0.3rem;
  }
  #choosePeopleNumber .peopleNumber div{
    display: flex;
    flex-direction: row;
    margin-top: 0.1rem;
    width: 100%;
    height: min-content;
  }
  #choosePeopleNumber .peopleNumber div span{
    width: 1.35rem;
    height: 0.5rem;
    line-height: 0.5rem;
    text-align: center;
    margin-right: 0.23rem;
    border: 1px solid #bcbcbc;
  }
  #choosePeopleNumber .byUser{
    width: 95%;
    height: min-content;
    margin-top: 0.3rem;
    margin-left: 0.53rem;
    display: flex;
    flex-direction: row;
  }
  #choosePeopleNumber .byUser span{
    margin-top: auto;
    margin-bottom: auto;
    font-size: 0.35rem;
  }
  #choosePeopleNumber .byUser input{
    text-align: center;
    margin-top: auto;
    margin-bottom: auto;
    height: 0.6rem;
    width: 4rem;
    border: #bcbcbc 1px solid;
    font-size: 0.4rem;
  }
  #choosePeopleNumber button {
    background: #e73822;
    color: white;
    width: 100%;
    height: 0.9rem;
    line-height: 0.9rem;
    text-align: center;
    font-size: 0.3rem;
    margin-top: auto;
  }
</style>
